from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from .fields import OrderField
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe


# Create your models here.

class Subject(models.Model):
    title = models.CharField(max_length=200, verbose_name='科目名称')
    slug = models.SlugField(max_length=200, unique=True, verbose_name='科目slug')

    class Meta:
        ordering = ['title']
        verbose_name_plural = '科目'
        verbose_name = '科目'

    def __str__(self):
        return self.title


class Course(models.Model):
    owner = models.ForeignKey(User, related_name='course_created', on_delete=models.CASCADE, verbose_name='课程讲师')
    subject = models.ForeignKey(Subject, related_name='courses', on_delete=models.CASCADE, verbose_name='科目')
    title = models.CharField(max_length=200, verbose_name='课程名称')
    slug = models.SlugField(max_length=200, unique=True, verbose_name='课程slug')
    overview = models.TextField(verbose_name='课程简介')
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    students = models.ManyToManyField(User, related_name='courses_joined', blank=True, verbose_name='学生')

    class Meta:
        ordering = ['-created']
        verbose_name = '课程'
        verbose_name_plural = '课程'

    def __str__(self):
        return self.title


class Module(models.Model):
    course = models.ForeignKey(Course, related_name='modules', on_delete=models.CASCADE, verbose_name='课程名称')
    title = models.CharField(max_length=200, verbose_name='课程名称')
    description = models.TextField(blank=True, verbose_name='课程描述')
    order = OrderField(for_fields=['course'], blank=True)

    def __str__(self):
        return f'{self.order}.{self.title}'

    class Meta:
        verbose_name = '课程模块'
        verbose_name_plural = '课程模块'
        ordering = ['order']


class Content(models.Model):
    module = models.ForeignKey(Module, related_name='contents', on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE,
                                     limit_choices_to={'model__in': ('text', 'file', 'image', 'video')})
    object_id = models.PositiveIntegerField()
    item = GenericForeignKey('content_type', 'object_id')
    order = OrderField(blank=True, for_fields=['module'])

    class Meta:
        ordering = ['order']


class ItemBase(models.Model):
    owner = models.ForeignKey(User, related_name='%(class)s_related', on_delete=models.CASCADE, verbose_name='课程讲师')
    title = models.CharField(max_length=250, verbose_name='课程名称')
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        abstract = True

    def __str__(self):
        return self.title

    def render(self):
        return render_to_string(f'courses/content/{self._meta.model_name}.html', {'item': self})


class Text(ItemBase):
    content = models.TextField(verbose_name='教学文本')


class File(ItemBase):
    file = models.FileField(upload_to='files', verbose_name='分发文件')


class Image(ItemBase):
    file = models.FileField(upload_to='images', verbose_name='图片')


class Video(ItemBase):
    url = models.URLField(verbose_name='视频地址')
